<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>

    <style>
        .origin{
            width: 209px;
            display: flex;
            justify-content: space-between;
        }
        .origin:nth-child(even){
            background-color: rgb(183, 236, 157);
        }
        .origin:nth-child(odd){
            background-color: rgb(185, 241, 237);
        }
        .origin.red{
            background-color:red;
            text-decoration: line-through;
        }

        .x{
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div id="app">
        <one @addtask="addTaskHandler"></one>
        <two v-for="(item,index) in list"
                :item="item" :index="index"
                :key="item.key"
                @dissapear="dissapearHandler"
                @remove="removeHandler"
        ></two>
    </div>

    <script>
        const componenta = {
            template:`
                    <div>
                        <input @keyup.enter='addtask' v-model='val' placeholder="请输入任务..."/></input>
                        <button @click="addtask">add</button>
                    </div>
            `,
            data(){
                return {
                    val:''
                }
            },
            methods:{
                addtask(){
                    if(this.val){
                        this.$emit('addtask',this.val)
                        this.val = ''; 
                    } 
                },
            }
        }

        const componentb = {
            template:`
                    <div  @click="dissapear" :class="['origin',{red:item.checked}]"> 
                        <div class="task-name">{{ item.name }}</div>
                        <div class="x" @click.stop="remove" >X</div>
                    </div>
            `,
            props:{
                item:{
                    type:Object,
                    default:() => ({})

                },
                index:{
                    type:Number,
                    default:() => 0
                }
            },
            methods:{
                dissapear(){
                    this.$emit('dissapear',this.index)
                },
                remove(){
                    this.$emit('remove',this.index)
                }
            }
        }



        let vm = new Vue({
            el:'#app',
            components:{
                'one' : componenta,
                'two' : componentb
            },
            data(){
                return {
                    list:[], // * 初始化任务列表
                }
            },
            methods:{
                addTaskHandler(v){
                        this.list.push({
                            name : v,
                            checked : false,
                            key:Math.random()
                        })
                        
                },
                dissapearHandler(index){
                    this.list[index].checked = !this.list[index].checked;
                },
                removeHandler(index){
                    this.list.splice(index,1);

                }
            }
        });
    </script>
</body>
</html> 